package cn.tedu.hospitalqd.service.impl;

import cn.tedu.hospitalqd.ex.ServiceException;
import cn.tedu.hospitalqd.mapper.PaymentMapper;
import cn.tedu.hospitalqd.pojo.dto.PaymentAddNewDTO;
import cn.tedu.hospitalqd.pojo.dto.PaymentUpdateDTO;
import cn.tedu.hospitalqd.pojo.entity.Payment;
import cn.tedu.hospitalqd.pojo.vo.PaymentListItemVO;
import cn.tedu.hospitalqd.pojo.vo.PaymentStandardVO;
import cn.tedu.hospitalqd.service.PaymentService;
import cn.tedu.hospitalqd.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentMapper paymentMapper;

    public PaymentServiceImpl() {
        log.debug("创建业务类对象：PaymentServiceImpl");
    }

    @Override
    public void addNew(PaymentAddNewDTO paymentAddNewDTO){
        log.debug("开始处理【添加项目缴费表】的业务，参数：{}", paymentAddNewDTO);
        String name = paymentAddNewDTO.getPName();
        int count = paymentMapper.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}", name, count);
        // 判断统计结果是否大于0
        if(count > 0){
            //是：名称被占用，抛出异常
            String message = "添加项目缴费表失败，项目缴费表名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }


        // 创建Payment对象
        Payment payment = new Payment();
        //调用BeanUtils.copyProperties()将参数对象的属性值复制到Album对象中
        BeanUtils.copyProperties(paymentAddNewDTO,payment);
        //调用albumMapper的int insert(Album album)方法将相册数据插入到数据库
        log.debug("即将执行插入数据，参数：{}",payment);
        int rows = paymentMapper.insert(payment);
        if (rows != 1) {
            String message = "添加项目缴费失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id){
        log.debug("开始处理【根据ID删除项目缴费表】的业务，参数：{}", id);

            PaymentStandardVO payment = paymentMapper.getStandardById(id);
            if (payment == null) {
                String message = "删除项目缴费表失败，尝试访问的数据不存在!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }

            log.debug("即将执行删除，参数：{}", id);
            int rows = paymentMapper.deleteById(id);
            if (rows != 1) {
                String message = "删除项目缴费表失败，服务器忙，请稍后再尝试！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_DELETE, message);
            }

    }

    @Override
    public void updateInfoById(Long id, PaymentUpdateDTO paymentUpdateDTO) {
        log.debug("开始处理【修改项目缴费表详情】的业务，ID：{}，新数据：{}", id, paymentUpdateDTO);
        PaymentStandardVO queryResult = paymentMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "修改项目缴费表详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int count = paymentMapper.countByNameAndNotId(id, paymentUpdateDTO.getPName());
        if (count > 0) {
            String message = "修改项目缴费表详情失败，项目缴费表名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Payment payment = new Payment();
        BeanUtils.copyProperties(paymentUpdateDTO, paymentUpdateDTO);
        payment.setId(id);
        log.debug("即将执行修改数据，参数：{}",payment);
        int rows = paymentMapper.update(payment);
        if (rows != 1) {
            String message = "修改项目缴费表详情失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public PaymentStandardVO getStandardById(Long id){
        log.debug("开始处理【根据ID查询项目缴费表详情】的业务，参数：{}",id);
        PaymentStandardVO queryResult = paymentMapper.getStandardById(id);
        if(queryResult == null){
            String message = "获取项目缴费表详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return queryResult;
    }

    @Override
    public List<PaymentListItemVO> list(){
        List<PaymentListItemVO> list = paymentMapper.list();
        return list;
    }

}
